package com.joker.aigc.chat.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joker.aigc.chat.domain.entity.RoomFriend;
import com.joker.aigc.domain.entity.User;
import com.joker.aigc.chat.domain.enums.NormalOrNoEnum;
import com.joker.aigc.chat.service.RoomFriendService;
import com.joker.aigc.chat.mapper.RoomFriendMapper;
import com.joker.aigc.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 20346
 * @description 针对表【room_friend(单聊房间表)】的数据库操作Service实现
 * @createDate 2023-12-17 11:26:08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RoomFriendServiceImpl extends ServiceImpl<RoomFriendMapper, RoomFriend> implements RoomFriendService {


    //    final RoomService roomService;
    final UserService userService;

    public RoomFriend getByKey(String key) {
        return lambdaQuery().eq(RoomFriend::getRoomKey, key).one();
    }

    public void restoreRoom(Long id) {
        lambdaUpdate()
                .eq(RoomFriend::getId, id)
                .set(RoomFriend::getStatus, NormalOrNoEnum.NORMAL.getStatus())
                .update();
    }

    public void disableRoom(String key) {
        lambdaUpdate()
                .eq(RoomFriend::getRoomKey, key)
                .set(RoomFriend::getStatus, NormalOrNoEnum.NOT_NORMAL.getStatus())
                .update();
    }

    public List<RoomFriend> listByRoomIds(List<Long> roomIds) {
        return lambdaQuery()
                .in(RoomFriend::getRoomId, roomIds)
                .list();
    }

    public RoomFriend getRoomFriendByRoomId(Long roomID) {
        return lambdaQuery()
                .eq(RoomFriend::getRoomId, roomID)
                .one();
    }

    /**
     * 获取 用户好友 的uid 集合
     *
     * @param values 房间信息
     * @param uid    用户id
     * @return 好友id集合
     */
    @Override
    public Set<Long> getFriendUidSet(Collection<RoomFriend> values, Long uid) {
        return values.stream()
                .map(a -> getFriendUid(a, uid))
                .collect(Collectors.toSet());
    }

    @Override
    public Long getFriendUid(RoomFriend roomFriend, Long uid) {
        return Objects.equals(uid, roomFriend.getUid1()) ? roomFriend.getUid2() : roomFriend.getUid1();
    }

    /**
     * 获取 好友的集合
     *
     * @param roomIds 房间号集合
     * @param uid     需要查询的 集合
     * @return roomId 以及该单聊群对应的 好友信息
     */
    @Override
    public Map<Long, User> getFriendRoomMap(List<Long> roomIds, Long uid) {
        if (CollectionUtil.isEmpty(roomIds)) {
            return new HashMap<>();
        }
        // 首先查出 好友集合
        List<RoomFriend> roomFriendList = listByRoomIds(roomIds);
        Map<Long, RoomFriend> roomFriendMap = roomFriendList.stream().collect(Collectors.toMap(RoomFriend::getId, item -> item));
        Set<Long> friendUidSet = getFriendUidSet(roomFriendMap.values(), uid); // 查出好友集合 uid集合
        List<User> users = userService.listByIds(friendUidSet);
        Map<Long, User> userBatch = users.stream().collect(Collectors.toMap(User::getId, item -> item));
        return roomFriendMap.values()
                .stream()
                .collect(Collectors.toMap(RoomFriend::getRoomId,
                        roomFriend -> {
                            Long friendUid = getFriendUid(roomFriend, uid);
                            return userBatch.get(friendUid);
                        }));
    }

}




